<template>
  <div class="container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="商品名称">
        <el-input size="small" v-model.trim="searchParams.orderProductName"></el-input>
      </el-form-item>
      <el-form-item label="商品编号">
        <el-input size="small" v-model.trim="searchParams.orderProductNo"></el-input>
      </el-form-item>
      <el-form-item label="订单号">
        <el-input size="small"  v-model.trim="searchParams.orderId"></el-input>
      </el-form-item>
      <el-form-item label="用户">
        <el-input size="small" v-model.trim="searchParams.nickname"></el-input>
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select v-model="searchParams.flowState" clearable size="small">
          <el-option label="全部" :value="99"></el-option>
          <el-option label="待支付" :value="0"></el-option>
          <el-option label="已支付" :value="1"></el-option>
          <el-option label="已取消" :value="-1"></el-option>
          <!-- <el-option label="已失效" :value="-2"></el-option>
          <el-option label="申请退款" :value="3"></el-option>
          <el-option label="退款完成" :value="4"></el-option>
          <el-option label="拒绝退款" :value="-4"></el-option> -->
        </el-select>
      </el-form-item>
      <el-form-item label="门店名称">
        <el-select class="el-select-option" v-model="searchParams.advertisingId" clearable filterable placeholder="请选择" size="small">
            <el-option
              v-for="item in allShopList"
              :key="item.advertisingId"
              :label="item.advertisingName"
              :value="item.advertisingId">
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <template>
          <div class="block">
            <el-date-picker
              size="small"
              @change="selectDate"
              v-model="createDate"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </div>
        </template>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="searchTable">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" size="small" @click="refresh">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="table-origin">
      <my-table ref="myTableRef" stripe border highlight-current-row align="center" size="medium" :columns="columns"
        :loader="loader" :page-size="pageSize" :page="page"
        :pagination="{layout:'->, total, sizes, prev, pager, next, jumper',pageSizes: [10, 30, 50, 100]}">
        <template v-slot:flowState="{row}">
          <el-tag size="mini" effect="dark" type="warning" v-if="row.flowState==0">待支付</el-tag>
          <el-tag size="mini" effect="dark" type="success" v-if="row.flowState==1">已支付</el-tag>
          <el-tag size="mini" effect="dark" type="danger" v-if="row.flowState==-1">已取消</el-tag>
          <el-tag size="mini" effect="dark" type="danger" v-if="row.flowState==-2">已失效</el-tag>
          <el-tag size="mini" effect="dark" type="warning" v-if="row.flowState==3">申请退款</el-tag>
          <el-tag size="mini" effect="dark" type="success" v-if="row.flowState==4">退款完成</el-tag>
          <el-tag size="mini" effect="dark" type="danger" v-if="row.flowState==-4">拒绝退款</el-tag>
        </template>
        <template v-slot:operate="{row}">
          <el-button plain size="mini" @click="showInfo(row)">详情</el-button>
          <el-button plain size="mini" v-if="row.flowState==3" type="danger" @click="refund(row)">退款</el-button>
        </template>
        <template v-slot:mallUserNickname="{row}">
          <div class="mallUserNickname">
            <img v-if="row.mallUser&&row.mallUser.avatarUrl" align="middle" :src="row.mallUser.avatarUrl" />
            <span>{{row.mallUser==null?"":row.mallUser.nickname}}</span>
           </div>
        </template>

        <!-- 单价 -->
        <template v-slot:orderProductPrice="{row}">
          <span>{{row.orderProductPrice | numFormatFilters()}}</span>
        </template>

        <!-- 优惠金额 -->
        <template v-slot:discountPrice="{row}">
          <span>{{row.discountPrice | numFormatFilters()}}</span>
        </template>
      
        <!-- 总价 -->
        <template v-slot:payment="{row}">
          <span>{{row.payment | numFormatFilters()}}</span>
        </template>

      </my-table>
    </div>
    <orderInfo ref="orderInfoRef"></orderInfo>
  </div>
</template>

<script>
  import orderInfo from './core/orderInfo.vue'
  import {
    mallOrderList,
    refundOk
  } from '@/api/mall.product.js'

  import {
    listAllShopList
  } from '@/api/advertising.js'

  import {
    numFormat
  } from '@/utils/common'
  export default {
    components: {
      orderInfo
    },
    data() {
      return {
        page: 1,
        pageSize:10,
        columns: [{
            label: '序号',
            type: 'index',
            index: (index) => {
              return ((this.page - 1) * this.pageSize + index + 1)
            },
            align: 'center',
            fixed: 'left',
            width: 50
          },
          {
            label: '订单号',
            prop: 'orderId',
            align: 'left',
            fixed: 'left',
            width: 200
          },
          {
            label: '门店名称',
            prop: 'advertising.advertisingName',
            align: 'left',
            width: 200
          },
          {
            label: '商品名称',
            prop: 'orderProductName',
            align: 'left',
            width: 200
          },
          {
            label: '商品编号',
            prop: 'orderProductNo',
            align: 'left',
            width: 150
          },
          {
            label: '单价',
            prop: 'orderProductPrice',
            align: 'right'
          },
          {
            label: '数量',
            prop: 'productNum',
            align: 'left',
            width: 50
          },
          {
            label: '优惠金额',
            prop: 'discountPrice',
            align: 'right',
          },
          {
            label: '应付总价',
            prop: 'payment',
            align: 'right'
          },
          {
            label: '创建时间',
            prop: 'createTime',
            align: 'left',
            width: 150
          },
          {
            label: '用户',
            prop: 'mallUserNickname',
            align: 'left',
            width: 150
          },
          {
            label: '付款时间',
            prop: 'payTime',
            align: 'left',
            width: 150
          },
          {
            label: '订单状态',
            prop: 'flowState',
            align: 'left',
            width: 90
          },
          {
            label: '退款原因',
            prop: 'refundMemo',
            align: 'left'
          },
          {
            label: '操作',
            prop: 'operate',
            align: 'left',
            fixed: 'right',
            width: 150
          }
        ],

        //下拉门店列表
        allShopList:[],

        searchParams: {
          orderId: undefined,
          orderProductName: undefined,
          orderProductNo: undefined,
          nickname: undefined,
          createTimeS: undefined,
          createTimeE: undefined,
          flowState: undefined,
          advertisingId:undefined
        },
        createDate: ''
      }
    },

    mounted() {
      this.loadAllShopList();
    },

    methods: {
      // 加载数据
      async loader(page, pageSize) {
        var params = Object.assign(this.searchParams, {
          page,
          pageSize
        });
        var res = await mallOrderList(params);
        this.page = page;
        this.pageSize = pageSize;
        return res.data;
      },

      // 门店列表下拉选
      async loadAllShopList(){
        var res = await listAllShopList();
        this.allShopList = res.data;
      },

      // 选择时间
      selectDate(createDate) {
        if(createDate != null && createDate.length == 2) {
          this.searchParams.createTimeS = createDate[0];
          this.searchParams.createTimeE = createDate[1];
        }
      },

      // 重置
      refresh() {
        this.createDate = '';
        this.searchParams = {
          orderId: undefined,
          orderProductName: undefined,
          orderProductNo: undefined,
          nickname: undefined,
          createTimeS: undefined,
          createTimeE: undefined,
          flowState: undefined,
          advertisingId: undefined
        }
        this.searchTable();
      },

      searchTable() {
        this.$refs.myTableRef.refresh(this.page);
      },
      // 详情
      showInfo(row) {
        this.$refs.orderInfoRef.open(row.orderId);
      },

      // 退款
      refund(row){
        this.$confirm('确认退款?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(async () => {
          var res = await refundOk(row.orderId);
          this.$utils.showSuccessMessage(res.msg);
          this.searchTable();
        });
      },




    },

    // 过滤器 - 处理金额
    filters:{
      numFormatFilters(value){
        return numFormat(value)
      }
    }
  }
</script>

<style scoped lang="scss">
  .container {
    padding: 15px;

    .el-form .el-form-item {
      margin-bottom: 5px;
      .el-select-option{
        height: !important 32px;
      }
    }

    .mallUserNickname{
      display: flex;
      align-items: center;
      img{
        width: 30px;
        height: 30px;
        border-radius: 30px;
        margin-right: 10px;
      }
      span{
        color: #000;
        font-size: 14px;
        width: calc(100% - 40px);
        overflow: hidden;
        text-overflow:ellipsis;
        white-space: nowrap;
      }
    }

    .table-origin{
      margin-top: 15px;
    }

  }
</style>
